package com.example.jiaju.controller;

import com.example.jiaju.dataobject.Host;
import com.example.jiaju.dataobject.UserHost;
import com.example.jiaju.dto.HostNameDto;
import com.example.jiaju.dto.UserNameDto;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.form.*;
import com.example.jiaju.repository.HostRepository;
import com.example.jiaju.service.HostService;
import com.example.jiaju.until.ResultVOUntil;
import com.example.jiaju.vo.ResultVO;
import com.example.jiaju.service.impl.UserHostServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/Hosts")
@CrossOrigin(
        origins = "*",
        maxAge = 3600,
        methods = {RequestMethod.GET, RequestMethod.POST}
)
public class UserHostController {
    @Autowired
    private UserHostServiceImpl userHostService;


    //查询用户所有主机
    @PostMapping(value = "/getHosts")
    public ResultVO<HostNameDto> getHosts(@RequestBody @Valid PhoneForm phoneForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        List<HostNameDto> hostNameDtoList= userHostService.findHostList(phoneForm.getPhone());

        return ResultVOUntil.success(hostNameDtoList);
    }

    //绑定主机
    @PostMapping(value = "/bindHost")
    public ResultVO<UserHost> bindHost(@RequestBody @Valid BindForm bindForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        UserHost userHost = userHostService.bindHost(bindForm.getPhone(),bindForm.getIeee());

        return ResultVOUntil.success(userHost);
    }

    //切换主机
    @PostMapping(value = "/switchHost")
    public ResultVO switchHost(@RequestBody @Valid BindForm bindForm,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        userHostService.switchHost(bindForm.getPhone(),bindForm.getIeee());

        return ResultVOUntil.success();
    }

    //管理员分配权限
    @PostMapping(value = "/allocateRole")
    public ResultVO allocateRole(@RequestBody @Valid AllocateForm allocateForm,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        userHostService.allocateRole(
                allocateForm.getAllocateUser(), allocateForm.getIeee(), allocateForm.getAcceptUser()
        );

        return ResultVOUntil.success();

    }

    //管理员转让权限
    @PostMapping(value = "/transferAdminRole")
    public ResultVO transferAdminRole(@RequestBody @Valid TransferForm transferForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        userHostService.transferAdminRole(
                transferForm.getTransferUser(), transferForm.getIeee(), transferForm.getAcceptUser()
        );

        return ResultVOUntil.success();

    }

    //管理员获取主机下所有用户
    @PostMapping(value = "/getUsers")
    public ResultVO<UserNameDto> getUsers(@RequestBody @Valid BindForm bindForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        List<UserNameDto> userNameDtoList= userHostService.findUserList(bindForm.getPhone(),bindForm.getIeee());

        return ResultVOUntil.success(userNameDtoList);
    }

    //管理员删除用户
    @PostMapping(value = "/deleteUser")
    public ResultVO deleteUser(@RequestBody @Valid DeleteForm deleteForm,BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        userHostService.deleteUser(deleteForm.getAdminUser(),deleteForm.getIeee(),deleteForm.getPhone());
        return ResultVOUntil.success();
    }
}
